/**
 * Created by yihua on 2017/2/26.
 */
var logoUrl = '';
$(document).ready(function () {
    //$("#logo").fileinput('refresh');
    $('#editModal').on('hidden.bs.modal', function (e) {
        $("#edit_brandform").find("input").val('');
        $("#edit_logo").fileinput('destroy');
    })
    $(".editbrand").each(function () {
        $(this).click(function () {
            //填充modal
            var brand_id = $(this).parent().find("td").eq(0).text();
            var brand_name = $(this).parent().find("td").eq(1).text();
            var is_show = $(this).parent().find("td").eq(2).text();
            var display_order = $(this).parent().find("td").eq(3).text();
            var logo = $(this).parent().find("a")[0].href;
            logoUrl = logo;
            $("#edit_brand_id").val(brand_id);
            $("#edit_brand_name").val(brand_name);
            $("#edit_is_show").val(is_show);
            $("#edit_display_order").val(display_order);
            var content = '<img src=' + logo + ' style="width:130px">';
            $("#edit_logo").fileinput('refresh', {
                uploadUrl: "/common/uploadimage",
                overwriteInitial: true,
                showUpload: false,
                uploadAsync: true,
                maxFileSize: 1500,
                showClose: false,
                showCaption: false,
                browseLabel: '',
                removeLabel: '',
                browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
                removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
                removeTitle: 'Cancel or reset changes',
                elErrorContainer: '#kv-avatar-errors-1',
                msgErrorClass: 'alert alert-block alert-danger',
                defaultPreviewContent: content,
                layoutTemplates: {main2: '{preview} ' + ' {remove} {browse}'},
                allowedFileExtensions: ["jpg", "png", "gif"]
            }).on('fileuploaded', function (event, data, id, index) {
                var ret = data.response;
                if (0 != ret.errno) {
                    toastr.error('上传失败', '', {positionClass: 'toast-bottom-center'});
                } else {
                    logoUrl = ret.data.url;
                    console.log(logoUrl);
                }
            }).on("filebatchselected", function (event, files) {
                $("#edit_logo").fileinput("upload");
            });
            $("#editModal").modal("show");
            //调用编辑接口
            $("#edit_confirm").unbind('click').click(function () {
                var options = {
                    url: '/home/shop/editbrand',
                    beforeSubmit: editvalidate,  // pre-submit callback
                    success: editshowResponse,  // post-submit callback
                    dataType: 'json',
                    type: 'post',
                    data: {
                        'brand_id': brand_id,
                        'logo': logoUrl,
                    }
                };
                $("#edit_brandform").ajaxSubmit(options)
            });
        });
    });
});


function editvalidate(formData, jqForm, options) {
    for (var i = 0; i < formData.length; i++) {
        //console.log(formData[i].name)
        if (formData[i].name == 'file') {
            break;
        }
        if (!formData[i].value) {
            toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
            return false;
        }
    }

    console.log(formData)
}


function editshowResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    } else {
        toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
        $("#editModal").modal("hide");
        window.location.reload();
    }
}
